import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'

const state = {
  token: getToken(),
  id:'',//用户id
  account:'',//账号
  name: '',//昵称
  phone:'',// 手机号
  avatar: '',//用户头像
  introduction: '',//简介
  roles: []
}

const mutations = {
  SET_TOKEN: (state, token) => {//token
    state.token = token
  },
  SET_ID: (state, id) => {//用户id
    state.id = id
  },
  SET_ACCOUNT: (state, account) => {//账号
    state.account = account
  },
  SET_NAME: (state, name) => {//昵称
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {//用户图像
    state.avatar = avatar
  },
  SET_PHONE:  (state, phone) => {//手机号
    state.phone = phone
  },
  SET_INTRODUCTION: (state, introduction) => {//介绍
    state.introduction = introduction
  },
  SET_ROLES: (state, roles) => {//用户角色
    state.roles = roles
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ userName: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          reject('Verification failed, please Login again.')
        }
        const { roleId } = data.account
        // roles must be a non-empty array
        if (!roleId) {
          reject('getInfo: roles must be a non-null array!')
        }

        const {id, name,iocn ,phone,remark}=data.user
        const {account}=data.account
        commit('SET_ROLES', ['admin'])
       commit('SET_ID', id)
        commit('SET_ACCOUNT', account)
        commit('SET_NAME', name)
        commit('SET_PHONE', phone)
        commit('SET_AVATAR', iocn)
        commit('SET_INTRODUCTION', remark)
        console.log('用户图像',data.account,account);
    // commit('SET_AVATAR', avatar)
        // commit('SET_INTRODUCTION', introduction)
        resolve(data.account)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resetRouter()

        // reset visited views and cached views
        // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
        dispatch('tagsView/delAllViews', null, { root: true })

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  },

  // dynamically modify permissions
  changeRoles({ commit, dispatch }, role) {
    return new Promise(async resolve => {
      const token = role + '-token'

      commit('SET_TOKEN', token)
      setToken(token)

      const { roles } = await dispatch('getInfo')

      resetRouter()

      // generate accessible routes map based on roles
      const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })

      // dynamically add accessible routes
      router.addRoutes(accessRoutes)

      // reset visited views and cached views
      dispatch('tagsView/delAllViews', null, { root: true })

      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
